package a_wait_notify;

/**
 * @author Cheng-02
 * @version V1.0
 * @Package a_wait_notify
 * @date 2024/4/7 下午6:48
 */
public class Baozipu {
    private int count = 0;
    private boolean flag;

    public synchronized void getCount() {
        //判断是否是false，即没有包子，则消费线程等待
        if (this.flag  == false)
            try {
                this.wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        //如果flag有值，就证明有包子，开始消费
        System.out.println("消费的count = " + count);
        //设置flag的状态
        this.flag=false;
        //唤醒生产消费
        this.notify();


    }

    public synchronized void setCount() {
        if (this.isFlag() == true)
            try {
                this.wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        //如果flag没有值，就生产
        count++;
        System.out.println("生产出来的count = " + count);
        //设置flag
        this.flag=true;
        //唤醒消费者消费
        this.notify();

    }

    public boolean isFlag() {
        return flag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }
}
